package com.web.web;

import static com.web.message.ErrorMessage.errorRequiredField;

import java.util.*;

import org.apache.log4j.*;
import org.tmatesoft.svn.core.*;

import com.database.objects.*;
import com.load.batch.*;
import com.load.ctrls.*;
import com.web.message.*;

public class LoadAction extends BatchExecutor {

	private static final long serialVersionUID = 1L;

	private static Logger logger = Logger.getLogger("load.repository");

	private ILoadVersionCtrl loadVersionCtrl;

	private ILoadTrackCtrl loadTrackCtrl;

	private Project project;

	private VersionRepository versionRepository;

	private TrackerRepository trackerRepository;

	private String repository;

	public String index() {
		return SUCCESS;
	}

	public void validateLoad() {
		if (project.getName() == null || project.getName().equals("")) {
			addActionError(errorRequiredField("Name"));
		}

		if (repository == null || repository.equals("")) {
			addActionError(errorRequiredField("Repository"));
		} else {
			if (repository.equals(Project.REPOSITORIES.get(0))) {
				if (versionRepository.getUrl() == null || versionRepository.getUrl().equals("")) {
					addActionError(errorRequiredField("URL"));
				} else if (versionRepository.getLogin() == null || versionRepository.getLogin().equals("")) {
					addActionError(errorRequiredField("Login"));
				} else if (versionRepository.getPassword() == null || versionRepository.getPassword().equals("")) {
					addActionError(errorRequiredField("Password"));
				} else if (versionRepository.getType() == null) {
					addActionError(errorRequiredField("Type"));
				} else if (!versionRepository.isValidateUrl()) {
					addActionError(ErrorMessage.malformedURL);
				} else if (!versionRepository.isValidateStartDate() || !versionRepository.isValidateFinishDate()) {
					addActionError(ErrorMessage.malformedDates);
				}
			} else if (repository.equals(Project.REPOSITORIES.get(1))) {
				if (trackerRepository.getDistribution() == null) {
					addActionError(errorRequiredField("Distribution"));
				}
			}
		}
	}

	public String load() {
		try {
			logger.info("START");
			logger.info("REPOSITORY: " + project.getName());
			logger.info("URL: " + versionRepository.getUrl());
			logger.info("LOGIN: " + versionRepository.getLogin());
			logger.info("PASSWORD: " + versionRepository.getPassword());

			project.setVersionRepository(versionRepository);
			project.setTrackerRepository(trackerRepository);
			if (repository.equals(Project.REPOSITORIES.get(0))) {
				loadVersionCtrl.load(project, this);
			} else if (repository.equals(Project.REPOSITORIES.get(1))) {
				loadTrackCtrl.load(project, this);
			}
			addActionMessage(SuccessMessage.successLoad);

			logger.info("FINISH");
			return SUCCESS;
		} catch (SVNAuthenticationException e) {
			addActionError(ErrorMessage.errorAuthentication);
			e.printStackTrace();
		} catch (Exception e) {
			addActionError(ErrorMessage.errorLoadProject);
			e.printStackTrace();
		}
		return INPUT;
	}

	public void setLoadVersionCtrl(ILoadVersionCtrl loadVersionCtrl) {
		this.loadVersionCtrl = loadVersionCtrl;
	}

	public void setLoadTrackCtrl(ILoadTrackCtrl loadTrackCtrl) {
		this.loadTrackCtrl = loadTrackCtrl;
	}

	public List<String> getRepositories() {
		return Project.REPOSITORIES;
	}

	public List<Type> getTypes() {
		return Type.selectableValues();
	}

	public List<Distribution> getDistributions() {
		return TrackerRepository.DISTRIBUTIONS;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public void setRepository(String repository) {
		this.repository = repository;
	}

	public VersionRepository getVersionRepository() {
		return versionRepository;
	}

	public void setVersionRepository(VersionRepository versionRepository) {
		this.versionRepository = versionRepository;
	}

	public TrackerRepository getTrackerRepository() {
		return trackerRepository;
	}

	public void setTrackerRepository(TrackerRepository trackerRepository) {
		this.trackerRepository = trackerRepository;
	}

}